package org.x.user.controller;

import com.mybatisflex.core.paginate.Page;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.x.common.config.Result;
import org.x.common.enums.LogAsync;
import org.x.user.dto.AclAssignDTO;
import org.x.user.dto.CreateRoleDTO;
import org.x.user.dto.RoleQueryDTO;
import org.x.user.dto.UpdateRoleDTO;
import org.x.user.service.ISysRoleService;
import org.x.user.vo.RoleDetailVO;
import org.x.user.vo.RoleInfoVO;
import org.x.user.vo.RoleVO;

/**
 * 角色表 控制层。
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@RestController
@RequestMapping("/api/role")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 获取角色列表(分页)
     */
    @GetMapping("/list")
    public Result QueryRoleList(@ModelAttribute @Valid RoleQueryDTO roleQueryDTO) {
        Page<RoleInfoVO> page = sysRoleService.QueryRoleList(roleQueryDTO);
        return Result.ok(page);
    }

    /**
     * 创建角色
     */
    @LogAsync("创建角色")
    @PostMapping("/create")
    public Result createRole(@RequestBody @Valid CreateRoleDTO createRoleDTO) {
        RoleVO roleVO = sysRoleService.createRole(createRoleDTO);
        return Result.ok(roleVO);
    }
    /**
     * 修改角色
     */
    @LogAsync("修改角色")
    @PutMapping("/update")
    public Result updateRole(@RequestBody @Valid UpdateRoleDTO updateRoleDTO) {
        RoleVO roleVO = sysRoleService.updateRole(updateRoleDTO);
        return Result.ok(roleVO);
    }
    /**
     * 删除角色
     */
    @LogAsync("删除角色")
    @DeleteMapping("/delete")
    public Result deleteRole(@RequestParam Long id) {
        RoleVO roleVO = sysRoleService.deleteRole(id);
        return Result.ok(roleVO);
    }
    /**
     * 获取角色详情
     */
    @LogAsync("获取角色详情")
    @GetMapping("/detail")
    public Result getDetail(@RequestParam Long id) {
        RoleDetailVO roleDetailVO = sysRoleService.getDetail(id);
        return Result.ok(roleDetailVO);
    }
    /**
     * 分配角色权限
     */
    @LogAsync("分配角色权限")
    @PostMapping("/assignAcl")
    public Result assignAcl(@RequestBody @Valid AclAssignDTO  aclAssignDTO) {
         RoleVO roleVO = sysRoleService.assignAcl(aclAssignDTO);
        return Result.ok(roleVO);
    }

}